/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author leehoa
 */
//public class EncryptDecrypt {
//    
//}
import java.security.MessageDigest;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class EncryptDecrypt {
    public static void main(String[] args) throws Exception {
        // here are your inputs
//        String keyString = "averylongtext!@$@#$#@$#*&(*&}{23432432432dsfsdf";
        String input = "Vietnam was then unified under a Communist government, but was politically isolated and impoverished. In 1986, the government initiated a series of economic and political reforms, which began Vietnam's path towards integration into the world economy.[9] By 20";
        String key = "verysimple";
        String keyString = "averylongtext!@$@#$#@$#*&(*&}{23432432432dsfsdf";
//        System.out.println("input: "+input);
//        byte[] en = EncrypData(input);
//        System.out.println("encrypt: "+new String(en));
//        byte[] de = DecryptData(en);
//        System.out.println("decrypt: "+new String(de));
        
        AESControl aes = new AESControl(keyString);
//        for(int i = 0; i< 10;i++){
//            System.out.println("number: "+i);
//            String temp_in = String.valueOf(i)+"__"+input;
//            String entext = aes.encryptData(temp_in);
//            System.out.println("encrypt: "+entext);
//            String detext = aes.decryptData(entext);
//            System.out.println("decrypt: "+detext);  
//            System.out.println("_____________________________________________________________________________________________________");  
//        }
        //step1
        AESControl aes1 = new AESControl(keyString);        
        String en1 = aes1.encryptData(input);        
        String en11 = aes1.encryptData(en1);
        
        String de1 = aes1.decryptData(en11);
        String de11 = aes1.decryptData(de1);
        
        System.out.println("keystring: "+keyString);          
        System.out.println("input: "+input);          
        System.out.println("en1: "+en1);          
        System.out.println("en11: "+en11);          
        System.out.println("de1: "+de1);          
        System.out.println("de11(input): "+de11);          
        
    }
    
    public static byte[] EncrypData(String input)throws Exception{
        // here are your inputs
        String keyString = "averylongtext!@$@#$#@$#*&(*&}{23432432432dsfsdf";
//        String input = "lethanhhai";
//        System.out.println("input: "+input);
        // setup AES cipher in CBC mode with PKCS #5 padding
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

        // setup an IV (initialization vector) that should be
        // randomly generated for each input that's encrypted
        byte[] iv = new byte[cipher.getBlockSize()];
//        for(int i=0; i<iv.length; i++) {
//            iv[i] = 1;
//        }
//        new SecureRandom().nextBytes(iv);
        IvParameterSpec ivSpec = new IvParameterSpec(iv);

        // hash keyString with SHA-256 and crop the output to 128-bit for key
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        digest.update(keyString.getBytes());
        byte[] key = new byte[16];
        System.arraycopy(digest.digest(), 0, key, 0, key.length);
        SecretKeySpec keySpec = new SecretKeySpec(key, "AES");

        // encrypt
        cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
        byte[] encrypted = cipher.doFinal(input.getBytes("UTF-8"));
        System.out.println("encrypted: " + new String(encrypted));

        // include the IV with the encrypted bytes for transport, you'll
        // need the same IV when decrypting (it's safe to send unencrypted)

        // decrypt
//        cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
//        byte[] decrypted = cipher.doFinal(encrypted);
//        System.out.println("decrypted: " + new String(decrypted, "UTF-8"));        
        return encrypted;
    }
    public static byte[] DecryptData(byte[] abc)throws Exception{
                // here are your inputs
        String keyString = "averylongtext!@$@#$#@$#*&(*&}{23432432432dsfsdf";
//        String input = "lethanhhai";
//        System.out.println("input: "+input);
        // setup AES cipher in CBC mode with PKCS #5 padding
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

        // setup an IV (initialization vector) that should be
        // randomly generated for each input that's encrypted
        byte[] iv = new byte[cipher.getBlockSize()];
//        for(int i=0; i<iv.length; i++) {
//            iv[i] = 1;
//        }
//        new SecureRandom().nextBytes(iv);
        IvParameterSpec ivSpec = new IvParameterSpec(iv);

        // hash keyString with SHA-256 and crop the output to 128-bit for key
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        digest.update(keyString.getBytes());
        byte[] key = new byte[16];
        System.arraycopy(digest.digest(), 0, key, 0, key.length);
        SecretKeySpec keySpec = new SecretKeySpec(key, "AES");

        // include the IV with the encrypted bytes for transport, you'll
        // need the same IV when decrypting (it's safe to send unencrypted)

        // decrypt
        cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
        cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
        byte[] decrypted = cipher.doFinal(abc);
        System.out.println("decrypted: " + new String(decrypted, "UTF-8"));        
        return decrypted;
        
//        // here are your inputs
//        String keyString = "averylongtext!@$@#$#@$#*&(*&}{23432432432dsfsdf";
//        // setup AES cipher in CBC mode with PKCS #5 padding
//        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
//
//        // setup an IV (initialization vector) that should be
//        // randomly generated for each input that's encrypted
//        byte[] iv = new byte[cipher.getBlockSize()];
//        new SecureRandom().nextBytes(iv);
//        IvParameterSpec ivSpec = new IvParameterSpec(iv);
//
//        // hash keyString with SHA-256 and crop the output to 128-bit for key
//        MessageDigest digest = MessageDigest.getInstance("SHA-256");
//        digest.update(keyString.getBytes());
//        byte[] key = new byte[16];
//        System.arraycopy(digest.digest(), 0, key, 0, key.length);
//        SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
//
//        // include the IV with the encrypted bytes for transport, you'll
//        // need the same IV when decrypting (it's safe to send unencrypted)
//
//        // decrypt
//        cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
//        byte[] decrypted = cipher.doFinal(abc);
//        System.out.println("decrypted: " + new String(decrypted, "UTF-8"));        
//        return decrypted;        
    }
}